package cn.net.shuxing.yifu.common.back.controller;

import cn.net.shuxing.yifu.common.bean.*;
import cn.net.shuxing.yifu.common.constant.ContentAdStateType;
import cn.net.shuxing.yifu.common.constant.FilePathContant;
import cn.net.shuxing.yifu.common.constant.OssConstant;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.service.*;
import cn.net.shuxing.yifu.common.util.*;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import static cn.net.shuxing.yifu.common.constant.FilePathContant.localUrl;

@RestController
@RequestMapping("/back/content")
public class ContentController {
    @Autowired
    private ContentAdService contentAdService;

    @Autowired
    private ContentAnService contentAnService;

    @Autowired
    private ContentSysService contentSysService;

    @Autowired
    private ContentNewsService contentNewsService;

    @Autowired
    private ContentFreshmanService contentFreshmanService;

    @Autowired
    private ContentQuestionService contentQuestionService;

    @Autowired
    private ContentHelperService contentHelperService;

    @Autowired
    private ContentOnlineService contentOnlineService;

    @Autowired
    private ContentProtocalService contentProtocalService;

    @Autowired
    private HelperClassService helperClassService;

    @Autowired
    private OssCommonService ossCommonService;

    @Autowired
    private JedisUtil jedisUtil;

    @RequestMapping("/ad/picture/upload")
    public BaseDto uploadAdPicture(@RequestParam("file") MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        String suffix = FileUtil.getFileExtName(fileName);
        InputStream inputStream = file.getInputStream();
        byte[] iconContent = new byte[(int) file.getSize()];
        inputStream.read(iconContent);
        inputStream.close();
        String bucketName = OssConstant.ossBucketName;
        String objectName = "adPicture-" + UUID.randomUUID().toString().substring(0, 10);
        ossCommonService.uploadFile(bucketName, objectName, iconContent, suffix);
        String url = ossCommonService.getUrl(bucketName, objectName);
        Map<String, String> map = new HashMap<>();
        map.put("s", objectName);
        map.put("url", url);
        return BaseDto.ok(map);
    }

    @RequestMapping("/an/content/picture/upload")
    public BaseDto uploadAnContentPicture(@RequestParam("file") MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        String suffix = FileUtil.getFileExtName(fileName);
        InputStream inputStream = file.getInputStream();
        byte[] iconContent = new byte[(int) file.getSize()];
        inputStream.read(iconContent);
        inputStream.close();
        String objectName = "anPicture-" + UUID.randomUUID().toString().substring(0, 15) + suffix;
        ossCommonService.uploadFile(OssConstant.ossBucketName, objectName, iconContent, suffix);
        String url = ossCommonService.getUrl(OssConstant.ossBucketName, objectName);
        Map<String, String> map = new HashMap<>();
        map.put("src", url);
        return BaseDto.ok(map);
    }


    @RequestMapping("/ad/launch")
    public void launchContentAd(Integer adId, HttpServletResponse response) throws IOException {
        ContentAd contentAd = contentAdService.getByPrimaryKeyWithoutComplete(adId);
        if (contentAd != null) {
            contentAd.setAdStateType(ContentAdStateType.LAUNCH_DELAYED);
            contentAdService.update(contentAd);
        }
        response.sendRedirect("listpage");
    }

    @RequestMapping("/ad/stop")
    public void stopContentAd(Integer adId, HttpServletResponse response) throws IOException {
        ContentAd contentAd = contentAdService.getByPrimaryKeyWithoutComplete(adId);
        if (contentAd != null) {
            contentAd.setAdStateType(ContentAdStateType.STOP_AT_ONCE);
            contentAd.setAdState(0);
            contentAdService.update(contentAd);
        }
        response.sendRedirect("listpage");
    }

    @RequestMapping("/ad/search")
    public BaseDto searchAd(String s_adName,
                            Integer s_adState,
                            @RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                            @RequestParam(defaultValue = "15", value = "pageSize") int pageSize) throws UnsupportedEncodingException {
        //Page page = PageHelper.startPage(pageNum,pageSize);
//        if (StringUtils.hasText(new String(s_adName.getBytes("ISO-8859-1"), "UTF-8"))) {
        if (StringUtils.hasText(s_adName)) {
            List<ContentAd> contentAdList = contentAdService.searchByAdName(s_adName);
            PageInfo pageInfo = new PageInfo<ContentAd>(contentAdList);
            //pageInfo.setTotal(page.getTotal());
            return BaseDto.ok(pageInfo);
        } else {
            List<ContentAd> contentAdList = contentAdService.getByAdState(s_adState);
            PageInfo<ContentAd> pageInfo = new PageInfo<>(contentAdList);
            //pageInfo.setTotal(page.getTotal());
            return BaseDto.ok(pageInfo);
        }
    }

    /**
     * 新增ad
     *
     * @param response
     */
    @RequestMapping("/ad/add")
    public BaseDto adInsert(HttpServletResponse response,
                            Integer adType,
                            String adLink,
                            String adPicture,
                            String startTime,
                            String endTime,
                            String adName,
                            Integer adWeight) throws UnsupportedEncodingException {
        ContentAd contentAd = new ContentAd();
        contentAd.setAdType(adType);
        contentAd.setAdLink(adLink);
        contentAd.setAdPicture(adPicture);
        LocalDateTime startDateTime = null;
        LocalDateTime endDateTime = null;
        if (StringUtils.hasText(startTime))
            startDateTime = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        else
            startDateTime = LocalDateTime.now();
        if (StringUtils.hasText(endTime))
            endDateTime = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        else
            endDateTime = LocalDateTime.now();
        contentAd.setStartTime(startDateTime);
        contentAd.setEndTime(endDateTime);
        contentAd.setAdName(adName);
        contentAd.setAdStateType(ContentAdStateType.STOP_AT_ONCE);
        contentAd.setAdWeight(adWeight);
        contentAdService.insert(contentAd);
        return BaseDto.ok("插入成功");
    }

    /**
     * 根据广告id 删除
     *
     * @param adId
     * @return
     */
    @RequestMapping("/ad/del")
    public String getTransferByUserTypeadDel(@Param("ad_id") int adId) {
        ContentAd byPrimaryKey = contentAdService.getByPrimaryKey(adId);
        byPrimaryKey.setDeleted(1);
        contentAdService.update(byPrimaryKey);
        return "删除成功";
    }

    /**
     * 编辑ad并返回list页面
     *
     * @param response
     * @param id
     * @param adPicture
     */
    @RequestMapping("/ad/update")
    public BaseDto adUpdate(HttpServletResponse response,
                            Integer id,
                            Integer adType,
                            String adLink,
                            String adPicture,
                            String startTime,
                            String endTime,
                            String adName,
                            Integer adWeight) throws IOException {
        ContentAd contentAd = contentAdService.getByPrimaryKey(id);
        contentAd.setAdType(adType);
        contentAd.setAdLink(adLink);
        contentAd.setAdPicture(adPicture);
        LocalDateTime startDateTime = null;
        LocalDateTime endDateTime = null;
        if (StringUtils.hasText(startTime))
            startDateTime = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        if (StringUtils.hasText(endTime))
            endDateTime = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        contentAd.setStartTime(startDateTime);
        contentAd.setEndTime(endDateTime);
//        contentAd.setAdName(new String(adName.getBytes("ISO-8859-1"), "UTF-8"));
        contentAd.setAdName(adName);
        contentAd.setAdWeight(adWeight);
        contentAdService.update(contentAd);
        return BaseDto.ok("更新成功");
    }


    /**
     * 查询所有广告
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/ad/list")
    public BaseDto adList(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                          @RequestParam(defaultValue = "15", value = "pageSize") int pageSize) {
        BaseDto baseDto = new BaseDto();
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<ContentAd> pageInfo = new PageInfo<>(contentAdService.getAll());
        for (ContentAd contentAd : pageInfo.getList()) {
            contentAd.setAdPicture(ossCommonService.getUrl(OssConstant.ossBucketName, contentAd.getAdPicture()));
        }
        baseDto.setData(pageInfo);
        return baseDto;
    }

    /**
     * 查询广告详情
     *
     * @param adId
     * @return
     */
    @RequestMapping("/ad/searchbyid")
    public BaseDto adDetail(int adId) {
        BaseDto baseDto = new BaseDto();
        baseDto.setData(contentAdService.getByPrimaryKey(adId));
        return baseDto;
    }


    /**
     * 添加系统公告
     *
     * @param response
     * @param sysTitle
     * @param sysContent
     * @throws IOException
     */
    @RequestMapping("/sys/add")
    public BaseDto sysInsert(HttpServletResponse response,
                             String sysTitle,
                             String sysDetail,
                             String sysLink,
                             int type,
                             String sysContent,
                             int isUp) throws IOException {
        ContentSys byPrimaryKey = new ContentSys();
        if (type == 0) {
            String objectName = UUID.randomUUID().toString().substring(0, 10);
//            byPrimaryKey.setSysLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(sysDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
            byPrimaryKey.setSysLink(localUrl + ossCommonService.uploadLocalFile(objectName, sysDetail.getBytes(), ".html"));
        } else {
            byPrimaryKey.setSysLink(sysLink);
        }
        // TODO: 2019/6/6 线上部署的时候必须得注释掉
//        byPrimaryKey.setSysTitle(new String(sysTitle.getBytes("ISO-8859-1"), "UTF-8"));
//        byPrimaryKey.setSysDetail(new String(sysDetail.getBytes("ISO-8859-1"), "UTF-8"));
//        byPrimaryKey.setSysContent(new String(sysContent.getBytes("ISO-8859-1"), "UTF-8"));
        byPrimaryKey.setSysTitle(sysTitle);
        byPrimaryKey.setSysDetail(sysDetail);
        byPrimaryKey.setSysContent(sysContent);
        byPrimaryKey.setSysSkimNum(0);
        byPrimaryKey.setType(type);
        byPrimaryKey.setState(isUp);
        contentSysService.insert(byPrimaryKey);
        // TODO: 2019/6/4 系统公告从redis中去获取。
        if (isUp == 1) {
            jedisUtil.sadd(RedisKeyCreator.stickSys(), JSON.toJSONString(byPrimaryKey));
        }
        return BaseDto.ok("添加成功");
    }

    /**
     * 根据公告id 删除
     *
     * @param sysId
     * @return
     */
    @RequestMapping("/sys/del")
    public void sysDel(HttpServletResponse response, @Param("sys_id") int sysId) throws IOException {
        BaseDto baseDto = new BaseDto();
        ContentSys byPrimaryKey = contentSysService.getByPrimaryKey(sysId);
        byPrimaryKey.setDeleted(1);
        contentSysService.update(byPrimaryKey);
        Iterator<String> sysContents = jedisUtil.smembers(RedisKeyCreator.stickSys()).iterator();
        while (sysContents.hasNext()) {
            String member = sysContents.next();
            ContentSys contentSys1 = JSON.parseObject(member, ContentSys.class);
            if (contentSys1.getId() == sysId) {
                jedisUtil.srem(RedisKeyCreator.stickSys(), member);
                break;
            }
        }
        response.sendRedirect("listpage");
    }


//    /**
//     * 更新公告内容并返回list页面
//     *
//     * @param response
//     * @param id
//     * @param skimNum
//     * @param sysTitle
//     * @param sysContent
//     */
//    @RequestMapping("/sys/Update")
//    public void sysUpdate(HttpServletResponse response,
//                          int id,
//                          int skimNum,
//                          String sysTitle,
//                          String sysDetail,
//                          String sysContent) throws IOException {
//        ContentSys byPrimaryKey = contentSysService.getByPrimaryKey(id);
//        byPrimaryKey.setSysTitle(sysTitle);
//        String objectName = UUID.randomUUID().toString().substring(0, 10);
//        String url = ossCommonService.uploadLocalFile(objectName, new String(sysDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html");
//        byPrimaryKey.setSysDetail(objectName);
//        byPrimaryKey.setSysContent(sysContent);
//        byPrimaryKey.setSysSkimNum(skimNum);
//        byPrimaryKey.setSysLink("inlineLink-" + objectName);
//        contentSysService.insert(byPrimaryKey);
//        response.sendRedirect("listpage");
//    }


    /**
     * 查询所有公告
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/sys/list")
    public BaseDto sysList(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                           @RequestParam(defaultValue = "15", value = "pageSize") int pageSize) {
        BaseDto baseDto = new BaseDto();
        PageInfo<ContentSys> pageInfo = new PageInfo<>(contentSysService.getAll());
        baseDto.setData(pageInfo);
        return baseDto;
    }

    /**
     * 更新公告详情
     *
     * @return
     */
    @RequestMapping("/sys/edit")
    public BaseDto sysDetail(HttpServletResponse response,
                             int id,
                             String sysTitle,
                             String sysDetail,
                             String sysLink,
                             int type,
                             String sysContent,
                             int isUp) throws IOException {
        ContentSys byPrimaryKey = new ContentSys();
        byPrimaryKey.setId(id);
        if (type == 0) {
            String objectName = sysLink.substring(sysLink.lastIndexOf('/') + 1, sysLink.lastIndexOf('.'));
//            byPrimaryKey.setSysLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(sysDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
            byPrimaryKey.setSysLink(localUrl + ossCommonService.uploadLocalFile(objectName, sysDetail.getBytes(), ".html"));
        } else {
            byPrimaryKey.setSysLink(sysLink);
        }
//        byPrimaryKey.setSysTitle(new String(sysTitle.getBytes("ISO-8859-1"), "UTF-8"));
//        byPrimaryKey.setSysDetail(new String(sysDetail.getBytes("ISO-8859-1"), "UTF-8"));
//        byPrimaryKey.setSysContent(new String(sysContent.getBytes("ISO-8859-1"), "UTF-8"));
        byPrimaryKey.setSysTitle(sysTitle);
        byPrimaryKey.setSysDetail(sysDetail);
        byPrimaryKey.setSysContent(sysContent);
        byPrimaryKey.setSysSkimNum(0);
        byPrimaryKey.setType(type);
        byPrimaryKey.setState(isUp);
        contentSysService.update(byPrimaryKey);

        Iterator<String> sysContents = jedisUtil.smembers(RedisKeyCreator.stickSys()).iterator();
        while (sysContents.hasNext()) {
            String member = sysContents.next();
            ContentSys contentSys1 = JSON.parseObject(member, ContentSys.class);
            if (contentSys1.getId() == byPrimaryKey.getId()) {
                jedisUtil.srem(RedisKeyCreator.stickSys(), member);
                break;
            }
        }
        if (isUp == 1) {
            jedisUtil.sadd(RedisKeyCreator.stickSys(), JSON.toJSONString(byPrimaryKey));
        }
        return BaseDto.ok("修改成功");
    }

    /**
     * 查询公告详情
     *
     * @param sysId
     * @return
     */
    @RequestMapping("/sys/search")
    public BaseDto sysDetail(int sysId) {
        BaseDto baseDto = new BaseDto();
        baseDto.setData(contentSysService.getByPrimaryKey(sysId));
        return baseDto;
    }

    /**
     * 查询咨询详情
     *
     * @param newsId
     * @return
     */
    @RequestMapping("/news/search")
    public BaseDto newsDetail(int newsId) {
        BaseDto baseDto = new BaseDto();
        baseDto.setData(contentNewsService.getByPrimaryKey(newsId));
        return baseDto;
    }


    /**
     * 更新咨询详情
     *
     * @return
     */
    @RequestMapping("/news/edit")
    public BaseDto newsDetail(HttpServletResponse response,
                              int id,
                              String newsTitle,
                              String newsDetail,
                              String newsContent,
                              String newsLink,
                              String newsSource,
                              int type) throws IOException {
        ContentNews byPrimaryKey = new ContentNews();
        byPrimaryKey.setId(id);
        if (type == 0) {
            String objectName = newsLink.substring(newsLink.lastIndexOf('/'), newsLink.lastIndexOf('.'));
//            byPrimaryKey.setNewsLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(newsDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
            byPrimaryKey.setNewsLink(localUrl + ossCommonService.uploadLocalFile(objectName, newsDetail.getBytes(), ".html"));
        } else {
            byPrimaryKey.setNewsLink(newsLink);
        }
//        byPrimaryKey.setNewsTitle(new String(newsTitle.getBytes("ISO-8859-1"), "UTF-8"));
//        byPrimaryKey.setNewsDetail(new String(newsDetail.getBytes("ISO-8859-1"), "UTF-8"));
//        byPrimaryKey.setNewsContent(new String(newsContent.getBytes("ISO-8859-1"), "UTF-8"));
//        byPrimaryKey.setNewsSource(new String(newsSource.getBytes("ISO-8859-1"), "UTF-8"));
        byPrimaryKey.setNewsTitle(newsTitle);
        byPrimaryKey.setNewsDetail(newsDetail);
        byPrimaryKey.setNewsContent(newsContent);
        byPrimaryKey.setNewsSource(newsSource);
        byPrimaryKey.setType(type);
        contentNewsService.update(byPrimaryKey);
        return BaseDto.ok("修改成功");
    }

    /**
     * 增加单页
     *
     * @param response
     * @param anTitle
     * @param anDetail
     * @param anContent
     * @throws IOException
     */
    @RequestMapping("/an/add")
    public BaseDto anInsert(HttpServletResponse response,
                            String anTitle,
                            String anDetail,
                            String anContent) throws IOException {
        ContentAn contentAn = new ContentAn();
//        contentAn.setAnTitle(new String(anTitle.getBytes("ISO-8859-1"), "UTF-8"));
        contentAn.setAnTitle(anTitle);

        String objectName = UUID.randomUUID().toString().substring(0, 10);
//        String fileName = ossCommonService.uploadLocalFile(objectName, new String(anDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html");
        String fileName = ossCommonService.uploadLocalFile(objectName, anDetail.getBytes(), ".html");
        contentAn.setAnLink(localUrl + fileName);
//        contentAn.setAnContent(new String(anContent.getBytes("ISO-8859-1"), "UTF-8"));
//        contentAn.setAnDetail(new String(anDetail.getBytes("ISO-8859-1"), "UTF-8"));
        contentAn.setAnContent(anContent);
        contentAn.setAnDetail(anDetail);
        contentAnService.insert(contentAn);
        return BaseDto.ok("添加成功");
    }

    /**
     * 根据单页id 删除
     *
     * @param anId
     * @return
     */
    @RequestMapping("/an/del")
    public void anDel(HttpServletResponse response, @Param("an_id") int anId) throws IOException {
        ContentAn byPrimaryKey = contentAnService.getByPrimaryKey(anId);
        byPrimaryKey.setDeleted(1);
        contentAnService.update(byPrimaryKey);
        response.sendRedirect("listpage");
    }

    /**
     * 更新单页信息并返回list页面
     *
     * @param response
     * @param id
     * @param anTitle
     * @param anLink
     * @param anDetail
     * @param anContent
     */
    @RequestMapping("/an/update")
    public BaseDto anUpdate(HttpServletResponse response,
                            int id,
                            String anTitle,
                            String anLink,
                            String anDetail,
                            String anContent) throws IOException {
        ContentAn byPrimaryKey = contentAnService.getByPrimaryKey(id);
        String objectName = anLink.substring(anLink.lastIndexOf('/'), anLink.lastIndexOf('.'));
//        String url = ossCommonService.uploadLocalFile(objectName, new String(anDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html");
//        byPrimaryKey.setAnDetail(new String(anDetail.getBytes("ISO-8859-1"), "UTF-8"));
//        byPrimaryKey.setAnContent(new String(anContent.getBytes("ISO-8859-1"), "UTF-8"));
//        byPrimaryKey.setAnTitle(new String(anTitle.getBytes("ISO-8859-1"), "UTF-8"));
        String url = ossCommonService.uploadLocalFile(objectName, anDetail.getBytes(), ".html");
        byPrimaryKey.setAnDetail(anDetail);
        byPrimaryKey.setAnContent(anContent);
        byPrimaryKey.setAnTitle(anTitle);
        byPrimaryKey.setAnLink(localUrl + url);
        contentAnService.update(byPrimaryKey);
        return BaseDto.ok("更新成功");
//        response.sendRedirect("listpage");
    }

    /**
     * 查询所有单页
     *
     * @return
     */
    @RequestMapping("/an/list")
    public BaseDto anList(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                          @RequestParam(defaultValue = "15", value = "pageSize") int pageSize) {
        BaseDto baseDto = new BaseDto();
        PageInfo<ContentAn> pageInfo = new PageInfo<>(contentAnService.getAll());
        baseDto.setData(pageInfo);
        return baseDto;
    }

    /**
     * 查询单页详情
     *
     * @param anId
     * @return
     */
    @RequestMapping("/an/edit")
    public BaseDto anDetail(int anId) {
        BaseDto baseDto = new BaseDto();
        ContentAn contentAn = contentAnService.getByPrimaryKey(anId);
        baseDto.setData(contentAnService.getByPrimaryKey(anId));
        return baseDto;
    }

    /**
     * 插入一条快讯并返回list页面
     *
     * @param response
     * @param newsTitle
     * @param newsContent
     * @throws IOException
     */
    @RequestMapping("/news/add")
    public BaseDto newsInsert(HttpServletResponse response,
                              int newsType,
                              String newsTitle,
                              String newsDetail,
                              String newsContent,
                              String newsLink,
                              String newsSource) throws IOException {
        ContentNews contentNews = new ContentNews();
        String objectName = "";
        if (newsType == 0) {
            objectName = UUID.randomUUID().toString().substring(0, 10);
//            contentNews.setNewsLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(newsDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
            contentNews.setNewsLink(localUrl + ossCommonService.uploadLocalFile(objectName, newsDetail.getBytes(), ".html"));
        } else {
            contentNews.setNewsLink(newsLink);
        }
        contentNews.setType(newsType);
//        contentNews.setNewsTitle(new String(newsTitle.getBytes("ISO-8859-1"), "UTF-8"));
//        contentNews.setNewsContent(new String(newsContent.getBytes("ISO-8859-1"), "UTF-8"));
//        contentNews.setNewsDetail(new String(newsDetail.getBytes("ISO-8859-1"), "UTF-8"));
//        contentNews.setNewsSource(new String(newsSource.getBytes("ISO-8859-1"), "UTF-8"));
        contentNews.setNewsTitle(newsTitle);
        contentNews.setNewsContent(newsContent);
        contentNews.setNewsDetail(newsDetail);
        contentNews.setNewsSource(newsSource);
        contentNewsService.insert(contentNews);
        return BaseDto.ok("添加成功");
    }

    /**
     * 根据单页id 删除
     *
     * @param newsId
     * @return
     */
    @RequestMapping("/news/del")
    public void newsDel(HttpServletResponse response, @Param("news_id") int newsId) throws IOException {
        ContentNews byPrimaryKey = contentNewsService.getByPrimaryKey(newsId);
        byPrimaryKey.setDeleted(1);
        contentNewsService.update(byPrimaryKey);
        response.sendRedirect("listpage");
    }

//    /**
//     * 更新快讯内容并返回list页面
//     *
//     * @param response
//     * @param id
//     * @param newsTitle
//     * @param newsContent
//     */
//    @RequestMapping("/news/Update")
//    public void newsUpdate(HttpServletResponse response,
//                           int id,
//                           String newsTitle,
//                           String newsDetail,
//                           String newsContent,
//                           String newsSource) throws IOException {
//        ContentNews contentNews = contentNewsService.getByPrimaryKeyWithoutComplete(id);
//        String objectName = UUID.randomUUID().toString().substring(0, 10);
//        ossCommonService.uploadLocalFile(objectName, new String(newsDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html");
//        contentNews.setNewsTitle(new String(newsTitle.getBytes("ISO-8859-1"), "UTF-8"));
//        contentNews.setNewsContent(new String(newsContent.getBytes("ISO-8859-1"), "UTF-8"));
//        contentNews.setNewsDetail(new String(objectName.getBytes("ISO-8859-1"), "UTF-8"));
//        contentNews.setNewsSource(new String(newsSource.getBytes("ISO-8859-1"), "UTF-8"));
//        contentNewsService.insert(contentNews);
//        response.sendRedirect("listpage");
//
//    }


    /**
     * 查询所有快讯
     *
     * @return
     */
    @RequestMapping("/news/list")
    public BaseDto newsList(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                            @RequestParam(defaultValue = "15", value = "pageSize") int pageSize) {
        BaseDto baseDto = new BaseDto();
        baseDto.setData(contentNewsService.getPageNews(pageNum, pageSize));
        return baseDto;
    }

//    /**
//     * 查询咨询详情
//     *
//     * @param newsId
//     * @return
//     */
//    @RequestMapping("/news/update")
//    public BaseDto newsUpdate(int newsId) {
//        BaseDto baseDto = new BaseDto();
//        baseDto.setData(contentNewsService.getByPrimaryKey(newsId));
//        return baseDto;
//    }

    /**
     * 公告置顶
     *
     * @return
     */
    @RequestMapping("/sys/puttop")
    public BaseDto putTopSys(Integer sysId, int state) {
        ContentSys contentSys = contentSysService.getByPrimaryKey(sysId);
        contentSys.setState(state);
        contentSysService.update(contentSys);
        Iterator<String> sysContents = jedisUtil.smembers(RedisKeyCreator.stickSys()).iterator();
        while (sysContents.hasNext()) {
            String member = sysContents.next();
            ContentSys contentSys1 = JSON.parseObject(member, ContentSys.class);
            if (contentSys1.getId() == sysId) {
                jedisUtil.srem(RedisKeyCreator.stickSys(), member);
                break;
            }
        }
        if (state == 1) {
            jedisUtil.sadd(RedisKeyCreator.stickSys(), JSON.toJSONString(contentSys));
        }
        BaseDto baseDto = new BaseDto();
        baseDto.setData("置顶成功");
        return baseDto;
    }

    /**
     * 查询所有新手指南
     *
     * @return
     */
    @RequestMapping("/freshman/list")
    public BaseDto freshmanList(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                                @RequestParam(defaultValue = "15", value = "pageSize") int pageSize) {
        BaseDto baseDto = new BaseDto();
        List<ContentFreshman> all = contentFreshmanService.getAll();
        baseDto.setData(all);
        return baseDto;
    }


    /**
     * 插入一条新手指南并返回list页面
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("/freshman/add")
    public BaseDto freshmanInsert(HttpServletResponse response,
                                  int freshmanType,
                                  String freshmanTitle,
                                  int freshmanTitleType,
                                  String freshmanDetail,
                                  String freshmanLink) throws IOException {
        ContentFreshman contentFreshman = new ContentFreshman();
        String objectName = "";
        if (freshmanType == 0) {
            objectName = UUID.randomUUID().toString().substring(0, 10);
//            contentFreshman.setFreshmanLink(localUrl + ossCommonService.uploadLocalFile(objectName, freshmanDetail.getBytes(), ".html"));
            contentFreshman.setFreshmanLink(localUrl + ossCommonService.uploadLocalFile(objectName, freshmanDetail.getBytes(), ".html"));
        } else {
            contentFreshman.setFreshmanLink(freshmanLink);
        }
        contentFreshman.setType(freshmanType);
        String title;
        switch (freshmanTitleType) {
            case 0:
                title = "如何激活钱包";
                break;
            case 1:
                title = "如何进行转账";
                break;
            case 2:
                title = "如何进行交易";
                break;
            default:
                return BaseDto.error("无此种类型！");
        }
        contentFreshman.setFreshmanTitle(title);
        contentFreshman.setTitleType(freshmanTitleType);
        contentFreshman.setFreshmanDetail(freshmanDetail);
        ContentFreshman condition = new ContentFreshman();
        condition.setTitleType(freshmanTitleType);
        contentFreshmanService.delete(condition);
        contentFreshmanService.insert(contentFreshman);
        return BaseDto.ok("添加成功");
    }

    /**
     * 根据id查询新手指南详情
     *
     * @param freshmanId
     * @return
     */
    @RequestMapping("/freshman/search")
    public BaseDto freshmanDetail(int freshmanId) {
        BaseDto baseDto = new BaseDto();
        baseDto.setData(contentFreshmanService.getByPrimaryKey(freshmanId));
        return baseDto;
    }


    /**
     * 根据新手指南id 删除
     *
     * @return
     */
    @RequestMapping("/freshman/del")
    public void freshmanDel(HttpServletResponse response, @Param("freshmanId") int freshmanId) throws IOException {
        ContentFreshman byPrimaryKey = contentFreshmanService.getByPrimaryKey(freshmanId);
        byPrimaryKey.setDeleted(1);
        contentFreshmanService.update(byPrimaryKey);
        response.sendRedirect("listpage");
    }


    /**
     * 更新新手指南
     *
     * @return
     */
    @RequestMapping("/freshman/edit")
    public BaseDto freshmanDetail(HttpServletResponse response,
                                  int id,
                                  String freshmanTitle,
                                  String freshmanDetail,
                                  String freshmanLink,
                                  int type) throws IOException {
        ContentFreshman byPrimaryKey = new ContentFreshman();
        byPrimaryKey.setId(id);
        if (type == 0) {
//            String objectName = UUID.randomUUID().toString().substring(0, 10);
//            byPrimaryKey.setFreshmanLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(freshmanDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
            String objectName = freshmanLink.substring(freshmanLink.lastIndexOf('/'), freshmanLink.lastIndexOf('.'));
            byPrimaryKey.setFreshmanLink(localUrl + ossCommonService.uploadLocalFile(objectName, freshmanDetail.getBytes(), ".html"));
        } else {
            byPrimaryKey.setFreshmanLink(freshmanLink);
        }
        String title;
//        switch (type) {
//            case 0:
//                title = "如何激活钱包";
//                break;
//            case 1:
//                title = "如何进行转账";
//                break;
//            case 2:
//                title = "如何进行交易";
//                break;
//            default:
//                return BaseDto.error("无此种类型！");
//        }
        byPrimaryKey.setFreshmanTitle(freshmanTitle);
//        byPrimaryKey.setFreshmanDetail(new String(freshmanDetail.getBytes("ISO-8859-1"), "UTF-8"));
        byPrimaryKey.setFreshmanDetail(freshmanDetail);
        byPrimaryKey.setType(type);
        contentFreshmanService.update(byPrimaryKey);
        return BaseDto.ok("修改成功");
    }

    /**
     * 查询所有常见问题
     *
     * @return
     */
    @RequestMapping("/question/list")
    public BaseDto questionList(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                                @RequestParam(defaultValue = "15", value = "pageSize") int pageSize) {
        BaseDto baseDto = new BaseDto();
        PageInfo<ContentQuestion> pageInfo = new PageInfo<>(contentQuestionService.getAll());
        baseDto.setData(pageInfo);
        return baseDto;
    }

    /**
     * 根据id查询常见问题详情
     *
     * @param questionId
     * @return
     */
    @RequestMapping("/question/search")
    public BaseDto questionDetail(int questionId) {
        BaseDto baseDto = new BaseDto();
        ContentQuestion contentQuestion = contentQuestionService.getLastRow();
        baseDto.setData(contentQuestion);
        return baseDto;
    }


    /**
     * 插入一条常见问题并返回list页面
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("/question/add")
    public BaseDto questionInsert(HttpServletResponse response,
                                  int questionType,
                                  String questionTitle,
                                  String questionetail,
                                  String questionLink) throws IOException {
        ContentQuestion contentQuestion = new ContentQuestion();
        String objectName = "";
        if (questionType == 0) {
            objectName = UUID.randomUUID().toString().substring(0, 10);
//            contentQuestion.setQuestionLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(questionetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
            contentQuestion.setQuestionLink(localUrl + ossCommonService.uploadLocalFile(objectName, questionetail.getBytes(), ".html"));
        } else {
            contentQuestion.setQuestionLink(questionLink);
        }
        contentQuestion.setType(questionType);
//        contentQuestion.setQuestionTitle(new String(questionTitle.getBytes("ISO-8859-1"), "UTF-8"));
//        contentQuestion.setQuestionDetail(new String(questionetail.getBytes("ISO-8859-1"), "UTF-8"));
        contentQuestion.setQuestionTitle(questionTitle);
        contentQuestion.setQuestionDetail(questionetail);
        contentQuestionService.insert(contentQuestion);
        return BaseDto.ok("添加成功");
    }


    /**
     * 根据常见问题id 删除
     *
     * @return
     */
    @RequestMapping("/question/del")
    public void questionDel(HttpServletResponse response, @Param("questionId") int questionId) throws IOException {
        ContentQuestion byPrimaryKey = contentQuestionService.getByPrimaryKey(questionId);
        byPrimaryKey.setDeleted(1);
        contentQuestionService.update(byPrimaryKey);
        response.sendRedirect("listpage");
    }


    /**
     * 更新常见问题
     *
     * @return
     */
    @RequestMapping("/question/edit")
    public BaseDto questionDetail(HttpServletResponse response,
                                  Integer id,
                                  String questionTitle,
                                  String questionDetail,
                                  String questionLink,
                                  Integer type) throws IOException {
//                String objectName = questionLink.substring(questionLink.lastIndexOf('/'), questionLink.lastIndexOf('.'));
        String objectName = "questionLink";
        if (id != null) {
            ContentQuestion byPrimaryKey = new ContentQuestion();
            byPrimaryKey.setId(id);
            if (type == 0) {
//                byPrimaryKey.setQuestionLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(questionDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
                byPrimaryKey.setQuestionLink(localUrl + ossCommonService.uploadLocalFile(objectName, questionDetail.getBytes(), ".html"));
            } else {
                byPrimaryKey.setQuestionLink(questionLink);
            }
//            byPrimaryKey.setQuestionTitle(new String(questionTitle.getBytes("ISO-8859-1"), "UTF-8"));
//            byPrimaryKey.setQuestionDetail(new String(questionDetail.getBytes("ISO-8859-1"), "UTF-8"));
            byPrimaryKey.setQuestionTitle(questionTitle);
            byPrimaryKey.setQuestionDetail(questionDetail);
            byPrimaryKey.setType(type);
            contentQuestionService.update(byPrimaryKey);
            return BaseDto.ok("修改成功");
        } else {
            ContentQuestion contentQuestion = new ContentQuestion();
//            String objectName = "";
//            objectName = UUID.randomUUID().toString().substring(0, 10);
//            contentQuestion.setQuestionLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(questionDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
//            contentQuestion.setQuestionTitle(new String(questionTitle.getBytes("ISO-8859-1"), "UTF-8"));
//            contentQuestion.setQuestionDetail(new String(questionDetail.getBytes("ISO-8859-1"), "UTF-8"));
            contentQuestion.setQuestionLink(localUrl + ossCommonService.uploadLocalFile(objectName, questionDetail.getBytes(), ".html"));
            contentQuestion.setQuestionTitle(questionTitle);
            contentQuestion.setQuestionDetail(questionDetail);
            contentQuestionService.insert(contentQuestion);
            return BaseDto.ok("添加成功");
        }
    }


    /**
     * 查询所有隐私协议
     *
     * @return
     */
    @RequestMapping("/protocal/list")
    public BaseDto protocalList(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                                @RequestParam(defaultValue = "15", value = "pageSize") int pageSize) {
        BaseDto baseDto = new BaseDto();
        PageInfo<ContentProtocal> pageInfo = new PageInfo<>(contentProtocalService.getAll());
        baseDto.setData(pageInfo);
        return baseDto;
    }

    /**
     * 根据id查询隐私协议详情
     *
     * @param protocalId
     * @return
     */
    @RequestMapping("/protocal/search")
    public BaseDto protocalDetail(Integer protocalId) {
        BaseDto baseDto = new BaseDto();
        ContentProtocal contentProtocal = contentProtocalService.getLastRow();
        baseDto.setData(contentProtocal);
        return baseDto;
    }

    /**
     * 插入一条隐私协议并返回list页面
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("/protocal/add")
    public BaseDto protocalInsert(HttpServletResponse response,
                                  int protocalType,
                                  String protocalTitle,
                                  String protocalDetail,
                                  String protocalLink) throws IOException {
        ContentProtocal contentProtocal = new ContentProtocal();
        String objectName = "";
        if (protocalType == 0) {
            objectName = UUID.randomUUID().toString().substring(0, 10);
//            contentProtocal.setProtocalLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(protocalDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
            contentProtocal.setProtocalLink(localUrl + ossCommonService.uploadLocalFile(objectName, protocalDetail.getBytes(), ".html"));
        } else {
            contentProtocal.setProtocalLink(protocalLink);
        }
        contentProtocal.setType(protocalType);
//        contentProtocal.setProtocalTitle(new String(protocalTitle.getBytes("ISO-8859-1"), "UTF-8"));
//        contentProtocal.setProtocalDetail(new String(protocalDetail.getBytes("ISO-8859-1"), "UTF-8"));
        contentProtocal.setProtocalTitle(protocalTitle);
        contentProtocal.setProtocalDetail(protocalDetail);
        contentProtocalService.insert(contentProtocal);
        return BaseDto.ok("添加成功");
    }

    /**
     * 根据隐私协议id 删除
     *
     * @return
     */
    @RequestMapping("/protocal/del")
    public void protocalDel(HttpServletResponse response, @Param("protocalId") int protocalId) throws IOException {
        ContentProtocal byPrimaryKey = contentProtocalService.getByPrimaryKey(protocalId);
        byPrimaryKey.setDeleted(1);
        contentProtocalService.update(byPrimaryKey);
        response.sendRedirect("listpage");
    }

    /**
     * 更新隐私协议
     *
     * @return
     */
    @RequestMapping("/protocal/edit")
    public BaseDto protocalDetail(HttpServletResponse response,
                                  Integer id,
                                  String protocalTitle,
                                  String protocalDetail,
                                  String protocalLink,
                                  Integer type) throws IOException {

        String objectName = "protocalLink";
        if (id != null) {
            ContentProtocal byPrimaryKey = new ContentProtocal();
            byPrimaryKey.setId(id);
            if (type == 0) {
//                String objectName = protocalLink.substring(protocalLink.lastIndexOf('/'), protocalLink.lastIndexOf('.'));
//                byPrimaryKey.setProtocalLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(protocalDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
                byPrimaryKey.setProtocalLink(localUrl + ossCommonService.uploadLocalFile(objectName, protocalDetail.getBytes(), ".html"));
            } else {
                byPrimaryKey.setProtocalLink(protocalLink);
            }
//        byPrimaryKey.setProtocalTitle(new String(protocalTitle.getBytes("ISO-8859-1"), "UTF-8"));
//        byPrimaryKey.setProtocalDetail(new String(protocalDetail.getBytes("ISO-8859-1"), "UTF-8"));
            byPrimaryKey.setProtocalTitle(protocalTitle);
            byPrimaryKey.setProtocalDetail(protocalDetail);
            byPrimaryKey.setType(type);
            contentProtocalService.update(byPrimaryKey);
            return BaseDto.ok("修改成功");
        } else {
            ContentProtocal contentProtocal = new ContentProtocal();
//            String objectName = "";
//            if (protocalType == 0) {
            objectName = UUID.randomUUID().toString().substring(0, 10);
//            contentProtocal.setProtocalLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(protocalDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
            contentProtocal.setProtocalLink(localUrl + ossCommonService.uploadLocalFile(objectName, protocalDetail.getBytes(), ".html"));
//            } else {
//                contentProtocal.setProtocalLink(protocalLink);
//            }
            contentProtocal.setType(type);
            contentProtocal.setProtocalTitle(protocalTitle);
            contentProtocal.setProtocalDetail(protocalDetail);
            contentProtocal.setProtocalTitle(protocalTitle);
            contentProtocal.setProtocalDetail(protocalDetail);
            contentProtocalService.insert(contentProtocal);
            return BaseDto.ok("添加成功");
        }
    }


    /**
     * 查询所有帮助中心
     *
     * @return
     */
    @RequestMapping("/helper/list")
    public BaseDto helperList(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                              @RequestParam(defaultValue = "15", value = "pageSize") int pageSize) {
        BaseDto baseDto = new BaseDto();
        PageInfo<ContentHelper> pageInfo = new PageInfo<>(contentHelperService.getAll());
        baseDto.setData(pageInfo);
        return baseDto;
    }

    /**
     * 根据id查询帮助中心详情
     *
     * @param helperId
     * @return
     */
    @RequestMapping("/helper/search")
    public BaseDto helperDetail(int helperId) {
        BaseDto baseDto = new BaseDto();
        baseDto.setData(contentHelperService.getByPrimaryKey(helperId));
        return baseDto;
    }

    /**
     * 插入一条帮助中心并返回list页面
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("/helper/add")
    public BaseDto helperInsert(HttpServletResponse response,
                                int helperType,
                                String helperTitle,
                                int helperClassId,
                                String helperClass,
                                String helperDetail,
                                String helperLink) throws IOException {
        ContentHelper contentHelper = new ContentHelper();
        String objectName = "";
        if (helperType == 0) {
            objectName = UUID.randomUUID().toString().substring(0, 10);
//            contentHelper.setHelperLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(helperDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
            contentHelper.setHelperLink(localUrl + ossCommonService.uploadLocalFile(objectName, helperDetail.getBytes(), ".html"));
        } else {
            contentHelper.setHelperLink(helperLink);
        }
        contentHelper.setType(helperType);
        contentHelper.setHelperTitle(helperTitle);
        contentHelper.setHelperDetail(helperDetail);
        contentHelper.setHelperClassId(helperClassId);
        contentHelper.setHelperClass(helperClass);
        contentHelperService.insert(contentHelper);
        return BaseDto.ok("添加成功");
    }

    /**
     * 根据帮助中心id 删除
     *
     * @return
     */
    @RequestMapping("/helper/del")
    public void helperDel(HttpServletResponse response, @Param("helperId") int helperId) throws IOException {
        ContentHelper byPrimaryKey = contentHelperService.getByPrimaryKey(helperId);
        byPrimaryKey.setDeleted(1);
        contentHelperService.update(byPrimaryKey);
        response.sendRedirect("listpage");
    }

    /**
     * 根据帮助分类id删除分类
     *
     * @return
     */
    @RequestMapping("/helperclass/del")
    public void helperClassDel(HttpServletResponse response, @Param("helperId") int helperId) throws IOException {
        HelperClass byPrimaryKey = helperClassService.getByPrimaryKey(helperId);
        byPrimaryKey.setDeleted(1);
        helperClassService.update(byPrimaryKey);
        response.sendRedirect("/back/content/helper/listclass");
    }

    /**
     * 更新隐私协议
     *
     * @return
     */
    @RequestMapping("/helper/edit")
    public BaseDto helperDetail(HttpServletResponse response,
                                int id,
                                String helperTitle,
                                String helperDetail,
                                String helperLink,
                                int type) throws IOException {
        ContentHelper byPrimaryKey = new ContentHelper();
        byPrimaryKey.setId(id);
        if (type == 0) {
            String objectName = helperLink.substring(helperLink.lastIndexOf('/'), helperLink.lastIndexOf('.'));
//            byPrimaryKey.setHelperLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(helperDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
            byPrimaryKey.setHelperLink(localUrl + ossCommonService.uploadLocalFile(objectName, helperDetail.getBytes(), ".html"));
        } else {
            byPrimaryKey.setHelperLink(helperLink);
        }
//        byPrimaryKey.setHelperTitle(new String(helperTitle.getBytes("ISO-8859-1"), "UTF-8"));
//        byPrimaryKey.setHelperDetail(new String(helperDetail.getBytes("ISO-8859-1"), "UTF-8"));
        byPrimaryKey.setHelperTitle(helperTitle);
        byPrimaryKey.setHelperDetail(helperDetail);
        byPrimaryKey.setType(type);
        contentHelperService.update(byPrimaryKey);
        return BaseDto.ok("修改成功");
    }

    /**
     * 查询所有帮助中心
     *
     * @return
     */
    @RequestMapping("/helperclass/list")
    public BaseDto helperClassList(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                                   @RequestParam(defaultValue = "15", value = "pageSize") int pageSize) {
        BaseDto baseDto = new BaseDto();
        PageInfo<HelperClass> pageInfo = new PageInfo<>(helperClassService.getAll());
        baseDto.setData(pageInfo);
        return baseDto;
    }

    /**
     * 插入一条帮助类别并返回list页面
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("/helperclass/add")
    public BaseDto helperClassInsert(HttpServletResponse response,
                                     String helperClass) {
        if (helperClass.isEmpty()) {
            return BaseDto.ok("添加失败");
        } else {
            HelperClass helperClass1 = new HelperClass();
            helperClass1.setHelperClass(helperClass);
            helperClassService.insert(helperClass1);
            return BaseDto.ok("添加成功");
        }
    }

    /**
     * 更新隐私协议
     *
     * @return
     */
    @RequestMapping("/helperclass/edit")
    public BaseDto helperClassEdit(HttpServletResponse response,
                                   int id,
                                   String helperClass) throws IOException {
        HelperClass helperClass1 = new HelperClass();
        helperClass1.setId(id);
        helperClass1.setHelperClass(helperClass);
        if (helperClassService.update(helperClass1) > 0) {
            return BaseDto.ok("修改成功");
        } else {
            return BaseDto.ok("修改失败");
        }
    }

    /**
     * 根据id查询帮助中心类别
     *
     * @param helperId
     * @return
     */
    @RequestMapping("/helperclass/search")
    public BaseDto helperClassSearch(int helperId) {
        BaseDto baseDto = new BaseDto();
        baseDto.setData(helperClassService.getByPrimaryKey(helperId));
        return baseDto;
    }


    /**
     * 查询所有隐私协议
     *
     * @return
     */
    @RequestMapping("/online/list")
    public BaseDto onlineList(@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,
                              @RequestParam(defaultValue = "15", value = "pageSize") int pageSize) {
        BaseDto baseDto = new BaseDto();
        PageInfo<ContentOnline> pageInfo = new PageInfo<>(contentOnlineService.getAll());
        baseDto.setData(pageInfo);
        return baseDto;
    }

    /**
     * 根据id查询隐私协议详情
     *
     * @param onlineId
     * @return
     */
    @RequestMapping("/online/search")
    public BaseDto onlineDetail(int onlineId) {
        BaseDto baseDto = new BaseDto();
        baseDto.setData(contentOnlineService.getByPrimaryKey(onlineId));
        return baseDto;
    }

    /**
     * 插入一条隐私协议并返回list页面
     *
     * @param response
     * @throws IOException
     */
    @RequestMapping("/online/add")
    public BaseDto onlineInsert(HttpServletResponse response,
                                int onlineType,
                                String onlineTitle,
                                String onlineDetail,
                                String onlineLink) throws IOException {
        ContentOnline contentOnline = new ContentOnline();
        String objectName = "";
        if (onlineType == 0) {
            objectName = UUID.randomUUID().toString().substring(0, 10);
//            contentOnline.setOnlineLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(onlineDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
            contentOnline.setOnlineLink(localUrl + ossCommonService.uploadLocalFile(objectName, onlineDetail.getBytes(), ".html"));
        } else {
            contentOnline.setOnlineLink(onlineLink);
        }
        contentOnline.setType(onlineType);
        contentOnline.setOnlineTitle(onlineTitle);
        contentOnline.setOnlineDetail(onlineDetail);
        contentOnlineService.insert(contentOnline);
        return BaseDto.ok("添加成功");
    }

    /**
     * 根据隐私协议id 删除
     *
     * @return
     */
    @RequestMapping("/online/del")
    public void onlineDel(HttpServletResponse response, @Param("onlineId") int onlineId) throws IOException {
        ContentOnline byPrimaryKey = contentOnlineService.getByPrimaryKey(onlineId);
        byPrimaryKey.setDeleted(1);
        contentOnlineService.update(byPrimaryKey);
        response.sendRedirect("listpage");
    }

    /**
     * 更新隐私协议
     *
     * @return
     */
    @RequestMapping("/online/edit")
    public BaseDto onlineDetail(HttpServletResponse response,
                                int id,
                                String onlineTitle,
                                String onlineDetail,
                                String onlineLink,
                                int type) throws IOException {
        ContentOnline byPrimaryKey = new ContentOnline();
        byPrimaryKey.setId(id);
        if (type == 0) {
            String objectName = onlineLink.substring(onlineLink.lastIndexOf('/'), onlineLink.lastIndexOf('.'));
//            byPrimaryKey.setOnlineLink(localUrl + ossCommonService.uploadLocalFile(objectName, new String(onlineDetail.getBytes("ISO-8859-1"), "UTF-8").getBytes(), ".html"));
            byPrimaryKey.setOnlineLink(localUrl + ossCommonService.uploadLocalFile(objectName, onlineDetail.getBytes(), ".html"));
        } else {
            byPrimaryKey.setOnlineLink(onlineLink);
        }
//        byPrimaryKey.setOnlineTitle(new String(onlineTitle.getBytes("ISO-8859-1"), "UTF-8"));
//        byPrimaryKey.setOnlineDetail(new String(onlineDetail.getBytes("ISO-8859-1"), "UTF-8"));
        byPrimaryKey.setOnlineTitle(onlineTitle);
        byPrimaryKey.setOnlineDetail(onlineDetail);
        byPrimaryKey.setType(type);
        contentOnlineService.update(byPrimaryKey);
        return BaseDto.ok("修改成功");
    }

    @RequestMapping("/excel/download")
    public void downloadFile(HttpServletResponse response) throws IOException {
        try {
//            Resource resource = new ClassPathResource(FilePathContant.localPath + "导入股东.xlsx");
            String filename = "导入股东.xlsx";
            File file = new File(FilePathContant.localPath + filename);
            if (file.exists()) {

                InputStream inputStream = new FileInputStream(file);
                //强制下载不打开
                response.setContentType("application/force-download");
                OutputStream out = response.getOutputStream();
                //使用URLEncoder来防止文件名乱码或者读取错误
                response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
                int b = 0;
                byte[] buffer = new byte[1000000];
                while (b != -1) {
                    b = inputStream.read(buffer);
                    if (b != -1) out.write(buffer, 0, b);
                }
                inputStream.close();
                out.close();
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}